﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using ForkCarRelationManager.db;
namespace ForkCarRelationManager.Business
{
    /*
     * 自动提醒业务类
     * */
    class RemindBI
    {
        DataPaging RemindPage;
        DataSet RemindDataSet;
        dbOperate dbo = new dbOperate();
        int pagecount;
        string datetime;
        public RemindBI(string datetime,int pagecount)
        {
            this.datetime=datetime;
            this.pagecount = pagecount;
        }
        /*
         * 获取分页页面
         * */
        public DataPaging GetRemindpage()
        {
            RemindPage = new DataPaging("RemindInfor a,MaintenRecord b,CarInfor c,ClientInfor d", pagecount, "(select sum(1) from (select * from RemindInfor where Str(RemindTime)='" + datetime + "') t where t.ID<=a.ID ) as 序号,d.ClientName,c.DeviceNum,iif(b.Ifmaintain=1,'保养',iif(b.Ifmaintain=0,'维修')) as 维修类型,a.RemindContext,d.ContactName,d.ContactPhone,b.MaintenDate,iif(a.Ifhandle=0,'处理',iif(a.Ifhandle=1,'已处理')) as Ifhandle ,a.Id as Id", "a.MaintenId=b.ID and b.CarId=c.ID and d.ID=c.ClientId and Str(a.RemindTime)='" + datetime + "'", "a");
            return RemindPage;
        }
        /*
         * 根据id查详情
         * */
        public DataTable Getdetailsbyrid(int id)
        {
            DataTable remindTable = null;

            string sqlmaintor = "select d.ClientName,c.DeviceNum,iif(b.Ifmaintain=1,'保养',iif(b.Ifmaintain=0,'维修')) as 维修类型,a.RemindContext,d.ContactName,d.ContactPhone,b.MaintenDate,a.RemindTime ,a.Id as Id from RemindInfor a,MaintenRecord b,CarInfor c,ClientInfor d where a.MaintenId=b.ID and b.CarId=c.ID and d.ID=c.ClientId and a.ID="+id;
            remindTable = dbo.GetDate(sqlmaintor);
            return remindTable;
       }
        public bool updatedetailbyid(int id,string datetime,string remindcontext)
        {
            string sql="update RemindInfor Set RemindContext='"+remindcontext+"',RemindTime=#"+datetime+"# where ID="+id;
            return dbo.UpdateAndInsert(sql);
        }
        /*
        * 获取总数据量
        * */
        public int GetDatacount()
        {
            int datacount=Convert.ToInt32( dbo.GetCount("RemindInfor a,MaintenRecord b,CarInfor c,ClientInfor d", "where a.MaintenId=b.ID and b.CarId=c.ID and d.ID=c.ClientId and Str(a.RemindTime)='" + datetime + "'"));
            return datacount;
        }
        /*
        * 根据id处理提醒信息
        * */
        public bool checkRemindInfor(string id)
        {

            return dbo.Delete("Update  RemindInfor Set Ifhandle =1 where id=" + id);

        }
        /*
         * 查找今日所有提醒，用于批量处理
         **/
        public DataTable findTodayRemind()
        {
           return dbo.GetDate("SELECT * from RemindInfor where RemindTime = #"+DateTime.Now.Date+"#");
        }
        /*
      * 根据id更新自动提醒时间
      * */
        public bool UpdateRemindInfor( string id)
        {
            string nextdatetime = DateTime.Now.AddDays(1).ToString("yyyy/M/d");
            return dbo.UpdateAndInsert("Update RemindInfor set RemindTime ='" +nextdatetime + "' where id=" + id);
        }
        public int getMaintenidbyid(int id)
        {
           
            string sql = "select MaintenId from RemindInfor where id=" + id;
            DataTable maintenTable = dbo.GetDate(sql);
            if (maintenTable != null)
            {
                if (maintenTable.Rows.Count != 0)
                {
                    return Convert.ToInt32(maintenTable.Rows[0][0].ToString());
                }
                else
                    return 0;
            }
            else
                return 0;
        }
    }
}
